﻿using System;
using System.Threading;

namespace 钓鱼
{
    class Program
    {
        static void Main(string[] args)
        {

            var 鱼竿 = new FishingRod();
            var jet = new FishingMan("jet");// 垂钓者
            jet.FishingRod = 鱼竿; //分配了一个鱼竿

            鱼竿.FishingEvent += jet.Update;//观察者

            while (jet.FishCount < 5)
            {
                jet.Fishing();
                Console.WriteLine("-------------------");
                //睡眠5s
                Thread.Sleep(5000);
            }
        }

        private static void 鱼竿_FishingEvent(FishType type)
        {
            throw new NotImplementedException();
        }
    }


    public enum FishType
    {
        鲫鱼,
        鲤鱼,
        黑鱼,
        青鱼,
        草鱼,
        鲈鱼
    }
    /// <summary>
    /// 鱼竿
    /// </summary>
    public class FishingRod
    {
        public delegate void FishingHandler(FishType type); //声明委托
        public event FishingHandler FishingEvent; //声明事件

        public void ThrowHook(FishingMan man)
        {
            Console.WriteLine("开始下钩！");

            //用随机数模拟鱼咬钩，若随机数为偶数，则为鱼咬钩
            if (new Random().Next() % 2 == 0)
            {
                var type = (FishType)new Random().Next(0, 5);
                Console.WriteLine("铃铛：叮叮叮，鱼儿咬钩了");
                if (FishingEvent != null)
                    FishingEvent(type);
            }
        }
    }

    /// <summary >
    ///     垂钓者（观察者）
    /// </summary >
    public class FishingMan
    {
        public FishingMan(string name)
        {
            Name = name;
        }

        public string Name { get; set; }
        public int FishCount { get; set; }

        /// <summary >
        /// 垂钓者自然要有鱼竿啊
        /// </summary >
        public FishingRod FishingRod { get; set; }

        public void Fishing()
        {
            this.FishingRod.ThrowHook(this);
        }

        public void Update(FishType type)
        {
            FishCount++;
            Console.WriteLine("{0}：钓到一条[{2}]，已经钓到{1}条鱼了！", Name, FishCount, type);
        }
    }

}
